package com.sjz.han.industry.activites;

import android.content.Intent;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.sjz.han.industry.MainActivity;
import com.sjz.han.industry.R;
import com.sjz.han.industry.base.BaseActivity;
import com.sjz.han.industry.entities.LoignEntity;
import com.sjz.han.industry.internetmanage.ReqestUrl;
import com.sjz.han.industry.utils.JsonUtils;
import com.sjz.han.industry.utils.SysUtils;
import com.sjz.han.industry.utils.ToastUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

//注册界面
public class RegistActivity extends BaseActivity implements View.OnClickListener{
    private EditText regist_phone_edt,regist_code_edt,regist_pwd_edt,regist_YQM_edt;
    private String phone="",code="",pwd="",yqm="";
    private TextView regist_getcode_tv;//获取验证码
    private Button regist_upload_btn;
    @Override
    protected int setLayoutId() {
        return R.layout.activity_regist;
    }

    @Override
    protected void initDatas() {

    }

    @Override
    protected void initViews() {
        regist_phone_edt=(EditText)this.findViewById(R.id.regist_phone_edt);
                regist_code_edt=(EditText)this.findViewById(R.id.regist_code_edt);
        regist_pwd_edt=(EditText)this.findViewById(R.id.regist_pwd_edt);
        regist_YQM_edt=(EditText)this.findViewById(R.id.regist_YQM_edt);

        regist_getcode_tv=(TextView)this.findViewById(R.id.regist_getcode_tv);//获取验证码
        regist_upload_btn=(Button)this.findViewById(R.id.regist_upload_btn);
    }

    @Override
    protected void setLisener() {
        regist_getcode_tv.setOnClickListener(this);
        regist_upload_btn.setOnClickListener(this);
    }

    @Override
    protected void onRetry() {

    }

    @Override
    public void onClick(View v) {
        phone=regist_phone_edt.getText().toString().trim();
                code=regist_code_edt.getText().toString().trim();
                pwd=regist_pwd_edt.getText().toString().trim();
                yqm=regist_YQM_edt.getText().toString().trim();
                switch (v.getId()){
                    case R.id.regist_getcode_tv://获取验证码
                        if(SysUtils.isNetworkAvailable(this)){
                            if(TextUtils.isEmpty(phone)){
                                ToastUtils.popUpToast("手机号不可为空！");
                            }else{
                                getCheckCode(phone);
                            }
                        }else{
                            ToastUtils.popUpToast(getResources().getString(R.string.text_nonet));
                        }
                        break;
                    case R.id.regist_upload_btn://提交
                        if(SysUtils.isNetworkAvailable(this)){
                            if(TextUtils.isEmpty(phone)){
                                ToastUtils.popUpToast("手机号不可为空！");
                            }else if(TextUtils.isEmpty(code)){
                                ToastUtils.popUpToast("验证码不可为空！");
                            }else if(TextUtils.isEmpty(pwd)){
                                ToastUtils.popUpToast("密码不可为空！");
                            }else if(TextUtils.isEmpty(yqm)){
                                ToastUtils.popUpToast("邀请码不可为空！");
                            }else{
                                regist();
                            }
                        }else{
                            ToastUtils.popUpToast(getResources().getString(R.string.text_nonet));
                        }
                        break;
                }
    }

    /**
     * ==========================获取验证码==============================
     * @param phone
     */
    private TimeCount timeCount=null;
    public void getCheckCode(String phone){
        timeCount = new TimeCount(60000,1000);
        timeCount.start();
        OkHttpUtils
                .post()
                .url(ReqestUrl.CHECKCODE_URL)
                .addParams("phone",phone)
                .addParams("type", "register")
                .tag(this)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                ToastUtils.popUpToast("请求超时");
            }
            @Override
            public void onResponse(String response, int id) {
                if(ReqestUrl.rebacRequestJson(response)!=null){
                    LoignEntity entity= JsonUtils.getObject(response,LoignEntity.class);
                    if(entity!=null&&entity.code==0){
                        ToastUtils.popUpToast(entity.msg);
                    }else{
                        ToastUtils.popUpToast("接口请求失败");
                    }
                }
            }
        });
    }
    //计时器
    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInfuture,long countDownInterval){
            super(millisInfuture,countDownInterval);
        }
        @Override
        public void onTick(long millisUntilFinished) {
            regist_getcode_tv.setText(millisUntilFinished/1000+"秒");
            regist_getcode_tv.setEnabled(false);
        }

        @Override
        public void onFinish() {
            regist_getcode_tv.setEnabled(true);
            regist_getcode_tv.setText("获取验证码");
        }
    }
    /**
     * ==========================获取验证码==============================
     */

    /**
     * ==========================账号密码登录==============================
     */
    public void regist(){
        showLoadingDialog("正在注册...");
        OkHttpUtils
                .post()
                .url(ReqestUrl.REGISTER_URL)
                .addParams("phone",phone)
                .addParams("code",code)
                .addParams("pwd",pwd)
                .addParams("inviteCode",yqm)
                .tag(this)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                hideLoadingDialog();
                ToastUtils.popUpToast("请求超时");
            }
            @Override
            public void onResponse(String response, int id) {
                hideLoadingDialog();
                if(ReqestUrl.rebacRequestJson(response)!=null){
                    LoignEntity entity= JsonUtils.getObject(response,LoignEntity.class);
                    if(entity!=null&&entity.code==0){
                        ToastUtils.popUpToast(entity.msg);
                        shareUtils.setIsFirstRunning(true);
                        shareUtils.setToken(entity.data.token);

                        startActivity(new Intent(RegistActivity.this, PayActivity.class));
                        finish();

                    }else{
                        ToastUtils.popUpToast(entity.msg);
                    }
                }
            }
        });
    }
}
